<%@ page contentType="text/html;charset=UTF-8" %>
<%@ include file="/WEB-INF/views/include/taglib.jsp" %>
<div class="panel-body">
    <div class="col-lg-12">
        <form:form id="inputForm" modelAttribute="patPrepayment" class="form-horizontal" data-parsley-validate="true"
                   onsubmit="return inputFormSaveLoad('mainCenterDiv','inputForm','${ctx}/finance/patPrepayment/save','${ctx}/finance/patPrepayment/depositAmountIndex');"
                   method="post">
            <form:hidden path="patientId" id="patientId" value="${patVisit.patientId}"/>
            <from:hidden path="type" value="1"/>
            <form:hidden path="visitId" id="visitId" value="${patVisit.id}"/>
            <form:hidden path="id" value="${patVisit.patPrepayment.id}"/>
            <input type="hidden" name="oneCardNo" id="oneCardNo"/>
            <%--<form:hidden path="type" value="1"/>--%>
            <div class="form-group">
                <label class="col-sm-3 control-label">住院号：</label>

                <div class="col-sm-8">
                    <form:input id="visitNo" path="patVisit.visitNo" value="${patVisit.visitNo}" disabled="true" class="form-control"
                                data-parsley-required="true" maxlength="100"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">姓名：</label>

                <div class="col-sm-8">
                    <input class="form-control" disabled="true"
                           value="${patVisit.patVisitIndex.name}" data-parsley-required="true"
                           maxlength="100"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">金额：</label>

                <div class="col-sm-8">
                    <form:input type="number" min="0" path="amount" id="amount" class="form-control" maxlength="100"
                                data-parsley-required="true"/>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">支付方式：</label>

                <div class="col-sm-8">
                    <form:select path="payWay" id="payWay" maxlength="40" class="form-control">
                        <form:options items="${fns:getDictList('PAY_WAY_DICT')}" htmlEscape="false"
                                      itemLabel="label" itemValue="value"/>
                    </form:select>
                </div>
            </div>
            <div class="form-group">
                <label class="col-sm-3 control-label">银行账户：</label>

                <div class="col-sm-8">
                    <form:input path="cardNo" class="form-control" maxlength="100"/>
                </div>
            </div>
            <div class="form-group">
                <div class="col-sm-offset-2 col-sm-10">
                    <input id="btnSubmit" class="btn btn-primary" type="submit" value="保 存">&nbsp;
                    <input id="btnSubmit" onclick="oneCardSolution('${patVisit.patPrepayment.id}')" class="btn btn-primary" type="button" value="一卡通交预交金">&nbsp;
                    <%--<input id="btnCancel" class="btn" type="button" value="作废">--%>
                </div>
            </div>
        </form:form>

    </div>
</div>
<script>
    function inputFormSaveLoad(divId, formId, url, hrefUrl){
        url=timestamp(url);
        hrefUrl=timestamp(hrefUrl);
        if (flag) {
            flag = false;
            formSubmitInput(formId);
            $("#" + formId + " input,#" + formId + " select").removeAttr("disabled");
            $("#" + formId).ajaxSubmit({
                type: 'post', // 提交方式 get/post
                url:url, // 需要提交的 url
                success: function(data) { // data 保存提交后返回的数据，一般为 json 数据
                    if (data.code == "success") {
                        toastr.success("缴纳病人预交金成功！");
                        loadDiv(divId, hrefUrl);
                        layer.open({
                            title: "提示",
                            type: 1,
                            area: ['300px', '150px'],
                            fixed: false, //不固定
                            skin: 'layui-layer-rim',
                            maxmin: true,
                            content:"需要打印预交金收据吗",
                            btn: ['确定', '取消'],
                            yes: function (index, layero) {
                                layer.close(index);
                                printPdf('${ctx}/finance/patPrepayment/prepaidPaymentPrint?id='+data.data);
                            }, no: function (index) {
                                layer.close(index);
                            }
                        });
                    }  else {
                        flag = true;
                        toastr.error(data.data);
                    }
                }
            });
        }
        return false;
    }

    function oneCardSolution(preId){
        var way = $("#payWay").val();
        var amount = $("#amount").val();
        var visitId = $("#visitId").val();
        var patientId = $("#patientId").val();
        var visitNo = $("#visitNo").val();
        if (way==3 || way=='3'){
            if (Number(amount)>0){
                if ($('#inputForm').parsley().validate()) {
                    var cardNo = readOneCard();
                    if (!cardNo) {
                        toastr.error("读卡失败!");
                        return false;
                    } else {
                        $("#oneCardNo").val(cardNo);
                        $.ajax({
                            type: "post",
                            url: "/a/med/medicalCardMaster/findByCardNo?cardNo=" + cardNo,
                            data: {r: Math.random()},
                            dataType: "json",
                            cache: false,
                            success: function (data) {
                                if (data) {
                                    /*判断一卡通余额是否大于当前消费费用*/
                                    if (Number(data.balance)>=Number(amount)){
                                        $.ajax({
                                            type: "GET",
                                            url: '${ctx}/finance/patPrepayment/saveOneCardSolution',
                                            'contentType': 'application/json',
                                            data:$("#inputForm").serialize(),
                                            dataType: "json",
                                            success: function (data) {
                                                if (data.code = "success") {
                                                    toastr.success("一卡通缴纳预交金成功！");
                                                    loadDiv('mainCenterDiv','${ctx}/finance/patPrepayment/depositAmountIndex')
                                                    layer.open({
                                                        title: "提示",
                                                        type: 1,
                                                        area: ['300px', '150px'],
                                                        fixed: false, //不固定
                                                        skin: 'layui-layer-rim',
                                                        maxmin: true,
                                                        content:"需要打印预交金收据吗？",
                                                        btn: ['确定', '取消'],
                                                        yes: function (index, layero) {
                                                            layer.close(index);
                                                            printPdf('${ctx}/finance/patPrepayment/prepaidPaymentPrint?id='+data.data);
                                                        }, no: function (index) {
                                                            layer.close(index);
                                                        }
                                                    });


                                                } else {
                                                    toastr.error(data.data);
                                                }
                                            },error: function (data) {
                                                toastr.error('网络连接错误,请检查网络');
                                            }
                                        });
                                    }else{
                                        toastr.info("一卡通余额不足，请充值后再缴费或者使用其他缴费方式！");
                                        return false;
                                    }
                                }
                            },
                            error: function(){
                                toastr.info('此卡未做登记或者信息不完整，请联系一卡通管理处！');
                                return false;
                            }
                        });

                    }
                }

            }else{
                toastr.info("预交金缴纳金额必须大于0！");
                return false;
            }

        }else {
            toastr.info("非一卡通支付方式，不能进行读取一卡通进行预交金支付!");
            return false;
        }

    }
</script>